<?php
	include('connect_db.php');
	/*if logged in, redirect to homepage */
	if (!checkLoggedin())
	{
		header('Location: login.php');
	}

	if(ISSET($_POST) && $_POST['amount'] > 0)
	{
	    $query = "UPDATE bills
					SET amount = ".$_POST['amount'].
					" , category_id = ".$_POST['category_id'].
					" , notes = '".trim($_POST['notes'])."'".
					" WHERE id = ".$_POST['bill_id'];
		
		mysql_query($query);

		$query = "DELETE FROM bills_users WHERE bill_id = ".$_POST['bill_id'];
		mysql_query($query);

		$bill_id = $_POST['bill_id'];
		for ($i = 0; $i < count($_POST['checked_fields']); $i++)
		{
			$query = "INSERT INTO bills_users (bill_id, user_id, shared_scale, if_paid) VALUES
					 	(".$bill_id.", "
					 	  .$_POST['checked_fields'][$i].", "
					 	  .floatval(1.0/(count($_POST['checked_fields'])+1.0))
					 	  .", 0)";
			mysql_query($query);
		}
	}
	header('Location: mybills.php');//a user not belong to this house
?>